package com.tdkj.service.shop.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.tdkj.dao.shop.IBeanShopDao;
import com.tdkj.model.decorate.BeanLayout;
import com.tdkj.model.decorate.BeanLayoutModuleConfig;
import com.tdkj.model.decorate.BeanModule;
import com.tdkj.model.decorate.BeanStyle;
import com.tdkj.model.shop.BeanShelf;
import com.tdkj.model.shop.BeanShop;
import com.tdkj.model.shop.BeanShopDecorateInfo;
import com.tdkj.model.shop.BeanShopFriendlyLink;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.BaseService;
import com.tdkj.service.shop.IBeanShopService;

@Component
public class BeanShopServiceImpl extends BaseService implements
		IBeanShopService {
	
	private IBeanShopDao beanShopDao;
    
	@Resource
	public void setBeanShopDao(IBeanShopDao beanShopDao) {
		this.beanShopDao = beanShopDao;
	}

	public IBeanShopDao getBeanShopDao() {
		return beanShopDao;
	}

	@Override
	public List<BeanShopFriendlyLink> getFriendlyLinksByUserId(Long userId) {
		BeanUser user = beanShopDao.findById(userId, BeanUser.class);
		return user.getShop().getLinks();
	}

	@Override
	public BeanShopFriendlyLink addFriendlyLinksByUserId(Long userId,
			Long frendId) {
		BeanShop shop = beanShopDao.findById(userId, BeanUser.class).getShop();
		BeanShop friend = beanShopDao.findById(frendId, BeanShop.class);
		
		BeanShopFriendlyLink bean = new BeanShopFriendlyLink();
		bean.setShop(shop);
		bean.setFriend(friend);
		
		beanShopDao.save(bean);
		
		return bean;
	}

	@Override
	public BeanShop getShopById(Long shopId) {
		BeanShop shop = beanShopDao.findById(shopId, BeanShop.class);
		shop.getUser().getUsername();
		shop.getLinks().size();
		
		for(BeanShelf shelf : shop.getShelfs()){
			shelf.getGoods().size();
		}
		
		shop.getShowcase().getGoods().size();
		shop.getComments().size();
		
		return shop;
	}
	
	@Override
	public BeanShop getShopByUserId(Long userId) {
		BeanUser user = beanShopDao.findById(userId, BeanUser.class);
		BeanShop shop = user.getShop();
		
		shop.getUser().getUsername();
		shop.getLinks().size();
		
		for(BeanShelf shelf : shop.getShelfs()){
			shelf.getGoods().size();
		}
		
		shop.getShowcase().getGoods().size();
		shop.getComments().size();
		
		return shop;
	}

	@Override
	public void saveDecorateInfo(Long userId) {
		BeanUser user = beanShopDao.findById(userId, BeanUser.class);
		BeanShop shop = user.getShop();
		
		BeanShopDecorateInfo infoTemp = shop.getDecorateInfoTemp();
		BeanShopDecorateInfo info = shop.getDecorateInfo();
		beanShopDao.delete(info);
		shop.setDecorateInfo(infoTemp);
		shop.setDecorateInfoTemp(null);
		
		Map<BeanLayout, BeanLayoutModuleConfig> configs = new HashMap<BeanLayout, BeanLayoutModuleConfig>();
		BeanLayout layout = infoTemp.getLayout();
		
		for(BeanLayout subLayout : layout.getChildren()){
			List<BeanModule> modules = new ArrayList<BeanModule>();
			for(BeanModule module: infoTemp.getConfigs().get(subLayout).getModules()){
				modules.add(beanShopDao.findById(module.getId(), BeanModule.class));
			}
			BeanLayoutModuleConfig config = new BeanLayoutModuleConfig();
			config.setLayout(subLayout);
			config.setModules(modules);
			
			configs.put(subLayout, config);
		}
		
		BeanShopDecorateInfo decorateInfo = new BeanShopDecorateInfo();
		decorateInfo.setLayout(layout);
		decorateInfo.setConfigs(configs);
		decorateInfo.setStyle(infoTemp.getStyle());
		beanShopDao.save(decorateInfo);
		
		shop.setDecorateInfoTemp(decorateInfo);
		
	}

	@Override
	public void createShop(Long userId) {
		BeanUser user = beanShopDao.findById(userId, BeanUser.class);
		if(user.getShop() == null || user.getShop().getId() != null){
			BeanShop shop = new BeanShop();
		}
	}

	@Override
	public void deleteDecorateInfo(Long userId) {
		BeanUser user = beanShopDao.findById(userId, BeanUser.class);
		BeanShop shop = user.getShop();
		
		BeanShopDecorateInfo info = shop.getDecorateInfo();
		BeanShopDecorateInfo infoTemp = shop.getDecorateInfoTemp();
		
		shop.setDecorateInfo(null);
		shop.setDecorateInfoTemp(null);
		
		beanShopDao.delete(info);
		beanShopDao.delete(infoTemp);
	}

	@Override
	public void changeLayout(Long userId, Long layoutId) {
		BeanUser user = beanShopDao.findById(userId, BeanUser.class);
		BeanShop shop = user.getShop();
		
		BeanLayout layout = beanShopDao.findById(layoutId, BeanLayout.class);
		
		int index = 0;
		for(BeanLayout subLayout : shop.getDecorateInfoTemp().getLayout().getChildren()){
			shop.getDecorateInfoTemp().getConfigs().get(subLayout).setLayout(layout.getChildren().get(index));
			index ++ ;
		}
		shop.getDecorateInfoTemp().setLayout(layout);
	}

}
